<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.aitu666.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <673011635@qq.com>
// +----------------------------------------------------------------------
namespace plugins\aitubaidu\controller; //Demo插件英文名，改成你的插件英文就行了
use cmf\controller\PluginRestBaseController;
use think\Db;

class ApiIndexController extends PluginRestBaseController
{

    public function index()
    {
        $this->success('success', ['hello' => 'hello ThinkCMF!']);
    }
    
    public function tui(){
    	$config = $this->getPlugin()->getConfig();//获取插件配置
    	$time = $config['baitutime'];
    	if($time==1){
			$post = Db::name('portal_post')->whereTime('published_time', 'today')->select();
    		// print_r($post);
    		$this->baidu($post,$config);
    		if($config['baidutuiurltian']){
    			$this->baidutian($config);
    		}
    		if($config['baidutuiurlzhou']){
    			$this->baiduzhou($post,$config);
    		}
    	}
    	
    }
    
    
    public function baidu($data,$config){
    	$url = $config['url'];
    	$api = $config['baidutuiurl'];
		$apiarticle = $config['apiarticle'];
   		if($config['urls']&&$config['baidutuiurls']){
    		$this->baidus($data,$config);
    	}
    	if($config['baidutuiurltian']){
    		$this->baidutian($config);
    	} 
    	if($config['baidutuiurlzhou']){
    		$this->baiduzhou($post,$config);
    	}
		$urls = array();
		foreach ($data as $isf){
			$urls[] = $url.$apiarticle.'/'.$isf['id'].'.html';
		}
		$ch = curl_init();
		$options =  array(
    CURLOPT_URL => $api,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => implode("\n", $urls),
    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
	curl_setopt_array($ch, $options);
	$result = curl_exec($ch);
	// echo $result;
	$this->success("推送成功！！");
    	
    }
    
    public function baidus($data,$config){
    	$url = $config['urls'];
    	$api = $config['baidutuiurls'];
		$apiarticle = $config['apiarticle'];
		$urls = array();
		foreach ($data as $isf){
			$urls[] = $url.$apiarticle.'/'.$isf['id'].'.html';
		}
		$ch = curl_init();
		$options =  array(
    CURLOPT_URL => $api,
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => implode("\n", $urls),
    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
	curl_setopt_array($ch, $options);
	$result = curl_exec($ch);
	// $this->success("https推送成功！！");    	
    	
    }    
    
    //天级推送
    public function baidutian($config){
    	$url = $config['url'];
    	$api = $config['baidutuiurltian'];
		$apiarticle = $config['apiarticle'];
		$urls = array();
		$data = Db::name('portal_post')->whereTime('published_time', 'today')->limit(10)->select();
		foreach ($data as $isf){
			$urls[] = $url.$apiarticle.'/'.$isf['id'].'.html';
		}
		$ch = curl_init();
		$options =  array(
    	CURLOPT_URL => $api,
    	CURLOPT_POST => true,
    	CURLOPT_RETURNTRANSFER => true,
    	CURLOPT_POSTFIELDS => implode("\n", $urls),
    	CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		// echo $result;
    	// $this->success("推送成功！！");  
    }    
    
    
    //周级推送
    public function baiduzhou($data,$config){
    	$url = $config['url'];
    	$api = $config['baidutuiurlzhou'];
		$apiarticle = $config['apiarticle'];
		$urls = array();
		foreach ($data as $isf){
			$urls[] = $url.$apiarticle.'/'.$isf['id'].'.html';
		}
		$ch = curl_init();
		$options =  array(
    	CURLOPT_URL => $api,
    	CURLOPT_POST => true,
	    CURLOPT_RETURNTRANSFER => true,
    	CURLOPT_POSTFIELDS => implode("\n", $urls),
    	CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		// echo $result;
    	}

}
